<template>
  <hips-view sub-header-fixed>
    <hips-nav-bar
      slot="header"
      title="Search 搜索框"
      :back-button="{
        showLeftArrow: true,
      }"
      @nav-bar-back-click="$router.back()"
    />

    <hips-tabs
      slot="sub-header"
      v-model="active"
    >
      <hips-tab title="标准用法" />
      <hips-tab title="Dsx用法" />
    </hips-tabs>

    <div v-show="!active">
      <demo-block title="基础用法">
        <div class="wrapper">
          <hips-search
            v-model="inputs.input0"
            placeholder="请输入搜索条件"
            show-clear-button
          />          
        </div>        
      </demo-block>

      <demo-block title="搜索图标在右侧">
        <div class="wrapper">
          <hips-search
            v-model="inputs.input1"
            placeholder="请输入搜索条件"
            show-clear-button
            icon-position="right"
          />
        </div>
      </demo-block>

      <demo-block title="按钮图标插槽">
        <div class="wrapper">
          <hips-search
            v-model="inputs.input4"
            placeholder="请输入搜索条件"
            show-clear-button
          >
            <template slot="search">
              Search
            </template>
            <template slot="clear">
              Clear
            </template>
          </hips-search>
        </div>
      </demo-block>

      <demo-block title="监听enter/search键">
        <div class="wrapper">
          <hips-search
            v-model="inputs.input2"
            placeholder="请输入搜索条件"
            enter-key
            show-clear-button
            @search="handleSearch"
          />
        </div>
      </demo-block>

      <demo-block title="禁用">
        <div class="wrapper">
          <hips-search
            v-model="inputs.input5"
            placeholder="请输入搜索条件"
            disabled
          />
        </div>
      </demo-block>

      <demo-block title="添加右侧按钮">
        <div class="wrapper">
          <hips-search
            v-model="inputs.input3"
            :right-button="rightButton"
            @right-click="handleRightClick"
          />
        </div>
      </demo-block>
    </div>
    <div v-show="active">
      <demo-block title="基础用法">
        <div class="wrapper">
          <hips-search :d-field="dsx.getQueryField('input0')" />
        </div>        
      </demo-block>

      <demo-block title="搜索图标在右侧">
        <div class="wrapper">
          <hips-search :d-field="dsx.getQueryField('input1')" />
        </div>
      </demo-block>

      <demo-block title="按钮图标插槽">
        <div class="wrapper">
          <hips-search :d-field="dsx.getQueryField('input4')">
            <template slot="search">
              Search
            </template>
            <template slot="clear">
              Clear
            </template>
          </hips-search>
        </div>
      </demo-block>

      <demo-block title="监听enter/search键">
        <div class="wrapper">
          <hips-search :d-field="dsx.getQueryField('input2')" />
        </div>
      </demo-block>

      <demo-block title="禁用">
        <div class="wrapper">
          <hips-search :d-field="dsx.getQueryField('input5')" />
        </div>
      </demo-block>

      <demo-block title="添加右侧按钮">
        <div class="wrapper">
          <hips-search :d-field="dsx.getQueryField('input3')" />
        </div>
      </demo-block>
    </div>
  </hips-view>
</template>

<script>
import BasicView from '@/mixin/basic-view';
import { Search, Tabs, Tab } from '@hips/vue-ui';
import Dsx from '@hips/plugin-dataset-vuex';

export default {
  components: {
    [Search.name]: Search,
    [Tabs.name]: Tabs,
    [Tab.name]: Tab,
  },
  mixins: [ BasicView ],
  data() {
    const _this = this;
    return {
      active: 0,
      inputs: {
        input0: '',
        input1: '',
        input2: '',
        input3: '',
        input4: '',
        input5: '',
      },
      dsx: new Dsx({
        moduleName: 'dsx-search',
        queryFields: [
          {
            name: 'input0',
            placeholder: '请输入搜索条件',
            defaultValue: '默认条件',
            showClearButton: true,
          },
          {
            name: 'input1',
            placeholder: '请输入搜索条件',
            showClearButton: true,
            iconPosition: 'right',
          },
          {
            name: 'input4',
            placeholder: '请输入搜索条件',
            showClearButton: true,
          },
          {
            name: 'input2',
            placeholder: '请输入搜索条件',
            enterKey: true,
            showClearButton: true,
            emits: {
              search() {
                _this.$hips.toast(
                  `input = ${JSON.stringify(
                    _this.dsx.getQueryField('input2').getValue(),
                  )}`,
                );
              },
            },
          },
          {
            name: 'input5',
            placeholder: '请输入搜索条件',
            disabled: true,
          },
          {
            name: 'input3',
            rightButton: {
              text: '取消',
              color: '#4D4D4D',
            },
            emits: {
              rightClick() {
                _this.handleRightClick();
              },
            },
          },
        ],
      }),
      rightButton: {
        text: '取消',
        color: '#4D4D4D',
      },
    };
  },
  methods: {
    handleSearch() {
      this.$hips.toast(`input = ${JSON.stringify(this.inputs.input2)}`);
    },
    handleRightClick() {
      this.$hips.toast(`click right button`);
    },
  },
};
</script>

<style scoped lang="stylus">
.wrapper {
    padding: 10px 15px;
    background-color: white;
    height: 50px;
    display: flex;
    align-items: center;
    box-sizing: border-box;
  }
</style>
